package cn.zsxmlv.modules.sys.service.impl;

import cn.zsxmlv.common.exception.RRException;
import cn.zsxmlv.common.utils.Constant;
import cn.zsxmlv.common.utils.PageUtils;
import cn.zsxmlv.common.utils.Query;
import cn.zsxmlv.modules.sys.dao.SysRoleDao;
import cn.zsxmlv.modules.sys.entity.SysRoleEntity;
import cn.zsxmlv.modules.sys.service.SysRoleMenuService;
import cn.zsxmlv.modules.sys.service.SysRoleService;
import cn.zsxmlv.modules.sys.service.SysUserRoleService;
import cn.zsxmlv.modules.sys.service.SysUserService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author: zs
 * @description: 系统角色管理
 * @date: 2019/2/27 10:42
 */
@Service
@Transactional
public class SysRoleServiceImpl extends ServiceImpl<SysRoleDao, SysRoleEntity> implements SysRoleService {

    @Resource
    private SysUserService sysUserService;
    @Resource
    private SysRoleMenuService sysRoleMenuService;
    @Resource
    private SysUserRoleService sysUserRoleService;

    @Override
    public PageUtils queryPage(Map<String, Object> map) {
        String roleName = (String) map.get("roleName");
        int createUserId = (int) map.get("createUserId");

        Page<SysRoleEntity> page = this.selectPage(new Query<SysRoleEntity>(map).getPage(),
                new EntityWrapper<SysRoleEntity>().like(StringUtils.isNotBlank(roleName), "role_name", roleName)
                        .eq(createUserId != 0, "create_user_id", createUserId));

        return new PageUtils(page);
    }

    @Override
    public void save(SysRoleEntity role) {
        // 验证角色名称是否重复
        SysRoleEntity sysRoleEntity = baseMapper.selectByRoleName(role.getRoleName());
        if (sysRoleEntity != null) {
            throw new RRException("该角色已存在");
        }

        // 保存角色
        role.setCreateTime(new Date());
        this.insert(role);

        // 检查权限
        checkAuth(role);

        // 保存角色关联的菜单
        sysRoleMenuService.saveOrUpdate(role.getId(), role.getMenuIdList());
    }

    @Override
    public void deleteBatch(Integer[] roleIds) {

        // 删除角色
        this.deleteBatchIds(Arrays.asList(roleIds));

        // 删除角色绑定的菜单表
        sysRoleMenuService.deleteBatch(roleIds);

        // 删除角色绑定的用户表
        sysUserRoleService.deleteBatch(roleIds);

    }

    @Override
    public List<SysRoleEntity> selectByUserId(Integer userId) {
        return baseMapper.selectByUserId(userId);
    }

    @Override
    public SysRoleEntity selectByRoleName(String roleName) {
        return baseMapper.selectByRoleName(roleName);
    }

    private void checkAuth(SysRoleEntity role) {
        if (role.getCreateUserId() == Constant.SUPER_ADMIN) {
            return;
        }

        List<Integer> list = sysUserService.queryAllMenuId(role.getCreateUserId());

        // 新增角色是否越权
        if (!list.containsAll(role.getMenuIdList())) {
            throw new RRException("新增角色的权限，已超出你的权限范围");
        }
    }

}
